{{ define "admin/role/auth.html" }}
{{ template "layouts/header.html" }}
<body style="background-color: #FFFFFF">
<div class="layui-form" lay-filter="authtree-submit-form" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{{ .id }}">
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限：</label>
        <div class="layui-input-block">
            <div id="LAY-auth-tree-index"></div>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="submit" id="layuiadmin-app-form-submit" value="确认添加">
    </div>
</div>
<script>
    layui.use(['layer','index','form','authtree'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,authtree = layui.authtree;

        // 模拟数据
        // var data =eval("(" + {{ .data }} + ")");
        var data ={{ .data }};
        // 初始化
        authtree.render('#LAY-auth-tree-index', data, {
            inputname: 'accessIds[]'
            ,layfilter: 'lay-check-auth'
             ,openall: true
            ,'theme': 'auth-skin-default'
            // ,'themePath': 'themes/'
            ,autowidth: true
            ,formFilter: 'authtree-submit-form' // 注意！！！如果不与其他插件render冲突，这个选填
        });
        //事件-提交
        form.on('submit(submit)', function(data){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var accessIds = '';
            $.each(data.field, function(i, item){
                if (i!='id'){
                    accessIds += item+','
                }
            });
            accessIds = accessIds.slice(0,-1)
            $.ajax({
                url:"/admin/role/doauth",
                method: "POST",
                data: {id:data.field.id,accessIds:accessIds},
                success: function(res) {
                    if(res.code == 0){
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1500,
                        },function () {
                            window.parent.location.reload(); //刷新父页面
                            parent.layer.close(index); //再执行关闭
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                },
                error: function() {
                    layer.msg("添加失败");
                }
            });
        });
    })
</script>
</body>
{{ end }}